home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Atari Mega Archive 1
/
Atari Mega Archive - Volume 1.iso
/
tex
/
td187src.lzh
/
FILEIO.I
< prev
next >
Wrap
Text File
|
1991-12-14
|
6KB
|
278 lines
IMPLEMENTATION MODULE FileIO;
FROM SYSTEM IMPORT ADDRESS, ADR;
IMPORT MagicStrings, MagicDOS, MagicConvert, MagicSys, mtAlerts, Diverses;
(**
IMPORT RTD;
**)
CONST NUL = 0C ;
NULL = MagicSys.LOC(0);
NULLL = MagicSys.sWORD(0);
LF = 12C ;
FF = 14C ;
CR = 15C ;
BufSize = 2048 ;
VAR InputBuffer : ARRAY [0..BufSize-1] OF MagicSys.LOC;
LastReadLoc : MagicSys.LOC;
LastReadWord : MagicSys.sWORD;
MaxPos, BufPos : INTEGER;
eof : BOOLEAN;
PROCEDURE FillBuffer(Filehandle : INTEGER);
VAR bytesread : MagicSys.lCARDINAL;
BEGIN
bytesread := MagicSys.CastToLCard(BufSize);
MagicDOS.Fread(Filehandle, bytesread, ADR(InputBuffer));
eof := bytesread <> MagicSys.CastToLCard(BufSize);
MaxPos := MagicSys.CastToInt(MagicSys.CastToCard(bytesread));
BufPos := 0;
(**
Variablen.CheckSentinel('Leaving FillBuffer');
**)
END FillBuffer;
PROCEDURE Rewrite(VAR filehandle : INTEGER;
REF filename : ARRAY OF CHAR);
BEGIN
filehandle := MagicDOS.Fcreate ( filename , {} );
(**
Variablen.CheckSentinel('Leaving Rewrite');
**)
END Rewrite;
PROCEDURE Fopen(VAR filehandle : INTEGER;
filemode : INTEGER;
REF filename : ARRAY OF CHAR) : BOOLEAN;
VAR Button : INTEGER;
pos : INTEGER;
str : ARRAY [0..127] OF CHAR;
num : ARRAY [0..4] OF CHAR;
BEGIN
filehandle := MagicDOS.Fopen ( filename , filemode );
IF filehandle < 6 THEN
str := "[4][ File-error #-?? !][ [OK ]" ;
pos := 0;
WHILE (str[pos]<>'?') DO
INC (pos);
END;
MagicConvert.IntToStr ( filehandle , 3 , num ) ;
str [ pos ] := num [ 1 ] ; str [ pos + 1 ] := num [ 2 ] ;
mtAlerts.SetIcon(mtAlerts.Disk);
Button := Diverses.Alert ( 1 , str ) ;
RETURN FALSE;
ELSE
RETURN TRUE;
END;
END Fopen;
PROCEDURE Reset(VAR filehandle : INTEGER;
REF filename : ARRAY OF CHAR);
BEGIN
AgainLine := FALSE;
AgainChar := FALSE;
LastReadLoc := NULL;
LastReadWord := NULLL;
BufPos := 0;
(**
RTD.Write('Reset', filename);
**)
IF Fopen ( filehandle, MagicDOS.Read, filename ) THEN
EOF := FALSE;
FillBuffer(filehandle);
(**
RTD.Message('File opened');
**)
ELSE
filehandle := -1;
LastReadLoc := NULL;
LastReadWord := NULLL;
EOF := TRUE;
END;
(**
Variablen.CheckSentinel('Leaving Reset');
**)
END Reset;
PROCEDURE Close(file : INTEGER);
VAR dum : INTEGER;
BEGIN
dum := MagicDOS.Fclose ( file ) ;
EOF := TRUE;
eof := TRUE;
BufPos := 0;
(**
Variablen.CheckSentinel('Leaving Close');
**)
END Close;
PROCEDURE ReadChar(File: INTEGER; VAR char : MagicSys.LOC );
BEGIN
(**
RTD.Message('IRC');
**)
IF NOT AgainChar THEN
IF BufPos >= MaxPos THEN
EOF := eof;
FillBuffer(File);
END;
IF NOT EOF THEN
LastReadLoc := InputBuffer[BufPos];
INC(BufPos);
ELSE
LastReadLoc := NULL;
END;
ELSE
AgainChar := FALSE;
END;
char := LastReadLoc;
(**
RTD.Message('ORC');
Variablen.CheckSentinel('Leaving ReadChar');
**)
END ReadChar;
PROCEDURE ReadWord (File: INTEGER; VAR word : MagicSys.sWORD );
VAR (*$Reg*) rec : RECORD
CASE : BOOLEAN OF
TRUE : barr : ARRAY [0..1] OF MagicSys.LOC; |
FALSE: wrd : MagicSys.sWORD; |
END;
END;
BEGIN
IF NOT EOF THEN
ReadChar(File, rec.barr[0]);
ReadChar(File, rec.barr[1]);
word := rec.wrd;
END;
END ReadWord;
PROCEDURE ReadNWords(File: INTEGER; N : CARDINAL; VAR wrdArr : ARRAY OF MagicSys.sWORD );
VAR c : CARDINAL;
BEGIN
(**
RTD.Message('IRNW');
**)
FOR c:=1 TO N DO
ReadWord(File, wrdArr[c-1]);
END;
(**
RTD.Message('ORNW');
**)
END ReadNWords;
PROCEDURE ReadNBytes(File: INTEGER; N : CARDINAL; VAR byteArr : ARRAY OF MagicSys.LOC );
VAR c : CARDINAL;
BEGIN
(**
RTD.Message('IRNB');
**)
FOR c:=1 TO N DO
ReadChar(File, byteArr[c-1]);
END;
(**
RTD.Message('ORNB');
**)
END ReadNBytes;
PROCEDURE WriteChar(File : INTEGER; char : MagicSys.LOC);
VAR temp : ARRAY [0..3] OF MagicSys.LOC;
len : CARDINAL ;
long : MagicSys.lCARDINAL ;
adr : ADDRESS ;
BEGIN
temp[0] := char;
long := 1;
adr := ADR(temp);
MagicDOS.Fwrite ( File , long , adr ) ;
(**
Variablen.CheckSentinel('Leaving WriteChar');
**)
END WriteChar;
PROCEDURE WriteLn (File: INTEGER; REF Line : ARRAY OF CHAR ) ;
VAR len : CARDINAL ;
long : MagicSys.lCARDINAL ;
adr : ADDRESS ;
buffer: ARRAY [0..255] OF CHAR;
BEGIN
MagicStrings.Assign(Line, buffer);
len := LENGTH(buffer);
IF UnixLine THEN
buffer[len] := LF;
INC(len);
ELSE
buffer[len ] := CR;
buffer[len+1] := LF;
INC(len, 2);
END;
buffer[len] := 0C;
len := LENGTH(buffer);
long := MagicSys.CastToLCard ( len ) ;
adr := ADR ( buffer ) ;
MagicDOS.Fwrite ( File , long , adr ) ;
(**
IF UnixLine THEN
WriteChar(File, LF);
ELSE
WriteChar(File, CR);
WriteChar(File, LF);
END;
**)
(**
Variablen.CheckSentinel('Leaving WriteLn ');
**)
END WriteLn ;
PROCEDURE ReadLn (File: INTEGER; VAR Line : ARRAY OF CHAR ) ;
TYPE chset = SET OF CHAR;
VAR c : CHAR;
i : INTEGER;
crlf : BOOLEAN;
MyLine : ARRAY [0..255] OF CHAR;
BEGIN
IF NOT AgainLine THEN
FOR i:= 0 TO 255 DO MyLine[i] := 0C; END;
IF NOT EOF THEN
i := 0;
crlf := FALSE;
(* Überliest leere Zeilen !!! *)
REPEAT
ReadChar(File, c);
UNTIL EOF OR NOT (c IN chset{CR,LF,FF});
REPEAT
IF c<>0C THEN
IF c=11C THEN c := ' '; END; (* TAB *)
IF c IN chset{CR,LF,FF} THEN
crlf := TRUE;
ELSE
MyLine[i] := c;
i := i + 1;
END;
END;
(* Um Unix-Zeilen-Trennung zu verarbeiten: LF statt CRLF *)
IF NOT crlf AND NOT EOF THEN
ReadChar(File, c);
END;
UNTIL EOF OR crlf;
END;
MagicStrings.Assign(MyLine, Line);
END;
AgainLine := FALSE;
(**
Variablen.CheckSentinel('Leaving ReadLn');
**)
END ReadLn;
BEGIN
UnixLine := FALSE;
END FileIO.